﻿using Cirrious.MvvmCross.Plugins.Sqlite;
using System.Collections.Generic;
using TeaLightV5.Foundation.BL;
using TeaLightV5.Foundation.DL;
using TeaLightV5.Foundation.Sal.Interfaces;

namespace TeaLightV5.Foundation.Dal
{
    public class RepositoryBase<T, TKey> : IRepositoryBase<T, TKey>
        where T : IEntity<TKey>, new()
        
    {
        public ITableQuery<T> GetTable()
        {
            return DatabaseBase.GetTable<T, TKey>();
        }

        public IEnumerable<T> GetAll()
        {
            return DatabaseBase.GetItems<T, TKey>();
        }

        public T GetById(TKey id)
        {
            return DatabaseBase.GetItem<T, TKey>(id);
        }

        public int Save(T item)
        {
            return DatabaseBase.SaveItem<T, TKey>(item);
        }

        public void SaveAll(IEnumerable<T> items)
        {
            DatabaseBase.SaveItems<T, TKey>(items);
        }

        public void UpdateAll(IEnumerable<T> items)
        {
            // todo
            DatabaseBase.ClearTable<T, TKey>();
            DatabaseBase.SaveItems<T, TKey>(items);
        }

        public int Delete(TKey id)
        {
            return DatabaseBase.DeleteItem<T, TKey>(id);
        }

        public void DeleteAll()
        {
            DatabaseBase.ClearTable<T, TKey>();
        }
    }
}